/**
 * Created by Administrator on 2017/3/17.
 */
/**
 * Created by Administrator on 2017/3/13.
 */
$(document).ready(function(){
    $("#registerName").blur(function () {
        var registerName = $("#registerName").val();
        if(registerName == ""){
            $(".your-name").text("用户名不能为空！请输入");
            $(".your-name").css("color","#FF3030");
        }else {
            $(".your-name").text("注册昵称");
            $(".your-name").css("color","#696969");
            $.ajax({
                url:"/register/check_name.action",
                type:"post",
                dataType:"json",
                data:{
                    registerName:registerName
                },
                success:function (data) {
                    if(data.checkResult == "user_exist"){
                        $(".your-name").text("用户名"+registerName+"已被占用！请重新输入");
                        $(".your-name").css("color","#FF3030");
                        $("#registerName").val("");
                    }
                },
                error:function (e) {
                    alert("程序错误："+e);
                }
            });
        }
    });
    $("#registerPhone").blur(function () {
        var registerPhone = $("#registerPhone").val();
        if(registerPhone == ""){
            $(".your-phone").text("手机号不能为空！请输入");
            $(".your-phone").css("color","#FF3030");
        }else {
            $(".your-phone").text("注册手机");
            $(".your-phone").css("color","#696969");
            $.ajax({
                url:"/register/check_phone.action",
                type:"post",
                dataType:"json",
                data:{
                    registerPhone:registerPhone
                },
                success:function (data) {
                    if(data.checkResult == "user_exist"){
                        $(".your-phone").text("手机号"+registerPhone+"已被注册！请重新输入");
                        $(".your-phone").css("color","#FF3030");
                        $("#registerPhone").val("");
                    }
                },
                error:function (e) {
                    alert("程序错误："+e);
                }
            });
        }
    });
    $("#registerEmail").blur(function () {
        var registerEmail = $("#registerEmail").val();
        if(registerEmail == ""){
            $(".your-email").text("邮箱不能为空！请输入");
            $(".your-email").css("color","#FF3030");
        }else {
            $(".your-email").text("注册邮箱");
            $(".your-email").css("color","#696969");
            $.ajax({
                url:"/register/check_email.action",
                type:"post",
                dataType:"json",
                data:{
                    registerEmail:registerEmail
                },
                success:function (data) {
                    if(data.checkResult == "user_exist"){
                        $(".your-email").text("邮箱"+registerEmail+"已被注册！请重新输入");
                        $(".your-email").css("color","#FF3030");
                        $("#registerEmail").val("");
                    }
                },
                error:function (e) {
                    alert("程序错误："+e);
                }
            });
        }
    });
    $("#registerPassword").blur(function () {
        var registerPassword = $("#registerPassword").val();
        if(registerPassword == ""){
            $(".your-password").text("密码不能为空！请输入账号");
            $(".your-password").css("color","#FF3030");
        }else {
            $(".your-password").text("登录密码");
            $(".your-password").css("color","#696969");
        }
    });
    $("#password_confirm").blur(function () {
        var password_confirm = $("#password_confirm").val();
        if(password_confirm == ""){
            $(".password-confirm").text("密码不能为空！请输入账号");
            $(".password-confirm").css("color","#FF3030");
        }else {
            $(".password-confirm").text("确认密码");
            $(".password-confirm").css("color","#696969");
        }
    });
    $("#sugnup_button").on("click",function () {
        var registerName = $("#registerName").val();
        var registerPhone = $("#registerPhone").val();
        var registerEmail = $("#registerEmail").val();
        var registerPassword = $("#registerPassword").val();
        var password_confirm = $("#password_confirm").val();
        var radio = document.getElementsByName("sex");
        /*var registerSex = '';
        for(var i=0;i<radio.length;i++){
            if(radio[i].checked == true){
                registerSex = radio[i].value;
            }
        }*/
        if(registerName == ""){
            $(".your-name").text("请输入用户名");
            $(".your-name").css("color","#FF3030");
        }else if(registerPhone == ""){
            $(".your-phone").text("请输入注册手机");
            $(".your-phone").css("color","#FF3030");
        }else if(registerEmail == ""){
            $(".your-email").text("请输入注册邮箱");
            $(".your-email").css("color","#FF3030");
        }else if(registerPassword == ""){
            $(".your-password").text("请输入密码");
            $(".your-password").css("color","#FF3030");
        }else if(password_confirm == ""){
            $(".password-confirm").text("请确认密码");
            $(".password-confirm").css("color","#FF3030");
        }else{
            $(".your-name").text("注册昵称");
            $(".your-name").css("color","#696969");
            $(".your-phone").text("注册手机");
            $(".your-phone").css("color","#696969");
            $(".your-email").text("注册邮箱");
            $(".your-email").css("color","#696969");
            $(".your-password").text("登录密码");
            $(".your-password").css("color","#696969");
            $(".password-confirm").text("确认密码");
            $(".password-confirm").css("color","#696969");
            if(registerPassword != password_confirm){
                $(".password-confirm").text("密码不匹配");
                $(".password-confirm").css("color","#FF3030");
            }else{
                $.ajax({
                    url:"/register/register.action",
                    type:"post",
                    dataType:"json",
                    data:{
                        registerName:registerName,
                        registerPhone:registerPhone,
                        registerEmail:registerEmail,
                        registerPassword:registerPassword
                    },
                    success:function (data) {
                        if(data.checkResult == "register_succeed"){
                            confirm("注册成功！");
                            window.location.href = "/login/login.action?name="+registerPhone+"&item=home";
                        }else if(data.checkResult == "register_failed"){
                            alert("注册失败！请重新注册");
                            $("#registerName").val("");
                            $("#registerPhone").val("");
                            $("#registerEmail").val("");
                            $("#registerPassword").val("");
                            $("#password_confirm").val("");
                        }
                    },
                    error:function (e) {
                        alert("程序错误："+e);
                    }
                });
            }

        }
    });
});